package ar.edu.itba.it.paw.web.transaction;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.itba.it.paw.domain.Auction;
import ar.edu.itba.it.paw.domain.Category;
import ar.edu.itba.it.paw.domain.EntityModel;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.domain.Transaction;
import ar.edu.itba.it.paw.repo.CategoryRepo;
import ar.edu.itba.it.paw.repo.TransactionRepo;
import ar.edu.itba.it.paw.web.MercadoLibre;
import ar.edu.itba.it.paw.web.base.BasePage;
import ar.edu.itba.it.paw.web.product.ProductDetailPage;

public class TransactionCategoryPage extends BasePage {

	@SpringBean
	private CategoryRepo categoryRepo;
	@SpringBean
	private TransactionRepo transactionRepo;
	
	private IModel<Category> categoryModel;
	
	public TransactionCategoryPage(Category category){
		
		categoryModel = new EntityModel<Category>(Category.class,category);
		
		setDefaultModel(new CompoundPropertyModel<Category>(categoryModel));

		if(category == null){
			add(new Label("chosenCategory","No category"));
		}else{
			add(new Label("chosenCategory",new PropertyModel<Category>(getDefaultModel(),"name")));
		}
		
		add(new FeedbackPanel("feedback").add(new SimpleAttributeModifier("style", "color: #FF0000")));
		Form<TransactionCategoryPage> categoryForm = new Form<TransactionCategoryPage>("categoryForm", new CompoundPropertyModel<TransactionCategoryPage>(this)) {

//			@Override
//			protected void onSubmit() {
//				setResponsePage(new TransactionCategoryPage(categoryModel.getObject()));
//			}
		};
		IModel<List<Category>> categoriesModel = new LoadableDetachableModel<List<Category>>() {
			@Override
			protected List<Category> load() {
				//TODO:Agregar opcion, todas
				return categoryRepo.getCategories();
			}
		};
		categoryForm.add(new DropDownChoice<Category>("formCategory",categoryModel ,categoriesModel,new ChoiceRenderer<Category>("name","id")).setRequired(true));
		categoryForm.add(new Button("listar", new ResourceModel("listar")));
		add(categoryForm);
		
		IModel<List<Transaction>> transactionsModel = new LoadableDetachableModel<List<Transaction>>() {
			@Override
			protected List<Transaction> load() {
				//TODO:chequear category
				Category categ = (Category)TransactionCategoryPage.this.getDefaultModelObject();
				if(categ == null || categ.getId() == 0){
					return new ArrayList<Transaction>();
				}
				return transactionRepo.getTransactionByCategory(categ);
			}
		};
		
		
		add(new PropertyListView<Transaction>("transactionList", transactionsModel) {
			
			@Override
			protected void populateItem(ListItem<Transaction> item) {
				item.add(new Label("buyer",new PropertyModel<String>(item.getModel(), "buyer.username")).add(new SimpleAttributeModifier("style", "color: #00FFFF")));
				item.add(new Label("seller",new PropertyModel<String>(item.getModel(), "seller.username")).add(new SimpleAttributeModifier("style", "color: #00FFFF")));
				//item.add(new Label("product", new ProductTypeModel(item.getModel())).add(new SimpleAttributeModifier("style","color: #FFFF00")));
				
				item.add(new Link<Transaction>("product", item.getModel()) {
					@Override
					public void onClick() {
						setResponsePage(new ProductDetailPage(getModelObject().getProduct()));
					}
				}.add(new Label("productName", new PropertyModel(item.getModel(),"product.name")).add(new SimpleAttributeModifier("style","color: #FFFF00"))));
				//.add(new Image("d_detail_image", MercadoLibre.DETAIL_ICON).add(new SimpleAttributeModifier("height","30px"))));
				item.add(new Label("date",new PropertyModel<String>(item.getModel(), "date")).add(new SimpleAttributeModifier("style", "color: #00FFFF")));
				item.add(new Label("quantity",new PropertyModel<String>(item.getModel(), "quantity")).add(new SimpleAttributeModifier("style", "color: #00FFFF")));
			}
		});
	}
}
